package com.zjk.dict.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjk.dict.mapper.DictMapper;
import com.zjk.pojo.product.TbDict;
import com.zjk.result.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: gfk
 * @create: 2022/11/2
 * @Description:
 * @FileName: DictService
 * @History:
 * @自定义内容：
 */
@Service
public class DictService {
    @Autowired
    private DictMapper dictMapper;

    public List<TbDict> getProductTypeList() {
        QueryWrapper<TbDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbDict::getCode,"product_type");
        queryWrapper.lambda().orderByAsc(TbDict::getSort);
    return   dictMapper.selectList(queryWrapper);
    }

    public List<TbDict> getRepaymentWayList() {
        QueryWrapper<TbDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbDict::getCode,"repayment_way");
        queryWrapper.lambda().orderByAsc(TbDict::getSort);
        return   dictMapper.selectList(queryWrapper);
    }

    public ResultResponse saveProductType(TbDict tbDict) {

        if (tbDict.getId()!=null){
            dictMapper.insert(tbDict);
            return new ResultResponse(true,"添加成功");
        }else {
            dictMapper.updateById(tbDict);
            return new ResultResponse(true,"修改成功");
        }

    }

    public TbDict getProductTypeValue(String value) {
        QueryWrapper<TbDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbDict::getCode,"product_type");
        queryWrapper.lambda().eq(TbDict::getValue,value);
        List<TbDict> tbDicts = dictMapper.selectList(queryWrapper);
        if (tbDicts.size()>0){
            return tbDicts.get(0);
        }
            return null;
    }

    public TbDict RepaymentWayByValue(String value) {
        QueryWrapper<TbDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbDict::getCode,"repayment_way");
        queryWrapper.lambda().eq(TbDict::getValue,value);
        List<TbDict> tbDicts = dictMapper.selectList(queryWrapper);
        if (tbDicts.size()>0){
            return tbDicts.get(0);
        }
        return null;
    }
}
